/*
 * Created by SharpDevelop.
 * User: Edward Wilde (http://blogs.charteris.com/edwardw)
 * Date: 01/07/2007
 * Time: 18:35
 * 
 */

using NUnit.Framework;
using System;
using Wax;

namespace WaxLibTest
{
	/// <summary>
	/// Tests all of the behaviour of the <see cref="TestManager"/ > class.
	/// </summary>
	[TestFixture]
	public class TestManagerTest
	{
		[Test]
		public void Constructor()
		{
			using (TestManager testManager = new TestManager())
			{
				Assert.IsNotNull(testManager.Browser, "TestManager.Browser not initialized correctly");
				Assert.IsNotNull(testManager.Tests, "TestManager.Tests not initialized correctly");
				Assert.AreEqual(0, testManager.Tests.Count, "TestManager.Tests not initialized correctly");
                Assert.IsNotNull(testManager.ExecutionContext, "Execution content in test manager should not be null.");
			}
		}

        /// <summary>
        /// Test that the TestManager can be run in a mode where errors are not thrown but collected
        /// for analysis by a host program
        /// </summary>
        [Test]
        public void CatchExceptions()
        {
            using (TestManager testManager = new TestManager())
            {
                testManager.ThrowErrors = false;
                testManager.Tests.Add(new Test());
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1NotExist.htm")));
                testManager.Tests[0].Commands.Add(CommandTest.CreateCommandGotoUrl(System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1NotExistAsWell.htm")));                
                testManager.Execute();

                Assert.AreEqual(2, testManager.Tests[0].Errors.Count);
            }
        }

        /// <summary>
        /// Tests that exceptions are thrown when encountered and <see cref="TestManager.ThrowErrors"/> it's set to it's default value of <c>false</c>.
        /// </summary>
        [Test, ExpectedException(typeof(ExecutionException))]
	    public void ExceptionsAreThrown()
	    {
            try
            {
                using (TestManager testManager = new TestManager())
                {
                    testManager.Tests.Add(new Test());
                    testManager.Tests[0].Commands.Add(
                        CommandTest.CreateCommandGotoUrl(
                            System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1NotExist.htm")));
                    testManager.Tests[0].Commands.Add(
                        CommandTest.CreateCommandGotoUrl(
                            System.IO.Path.Combine(System.Environment.CurrentDirectory, "testpage1NotExistAsWell.htm")));
                    testManager.Execute();
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("Exception: " + exception.GetType().Name + " was thrown.");
                throw;
            }
	    }

        private int eventCalledCount;

        /// <summary>
        /// Tests the execution started event gets called.
        /// </summary>
        [Test]
        public void TestStartedEvent()
        {
            eventCalledCount = 0;

            using (TestManager testManager = new TestManager())
            {
                testManager.TestStarted += new EventHandler(TestExecutionEvent);
                Test test = new Test();
                testManager.Tests.Add(test);
                testManager.Tests.Add(test);
                testManager.Execute();
            }

            // Should have been called twice as we have defined 2 tests.
            Assert.AreEqual(2, eventCalledCount);
        }

        /// <summary>
        /// Tests the execution completed event gets called.
        /// </summary>
        [Test]
        public void TestCompletedEvent()
        {
            eventCalledCount = 0;
            
            using (TestManager testManager = new TestManager())
            {
                testManager.TestCompleted += new EventHandler(TestExecutionEvent);
                Test test = new Test();
                testManager.Tests.Add(test);
                testManager.Tests.Add(test);
                testManager.Execute();
            }

            // Should have been called twice as we have defined 2 tests.
            Assert.AreEqual(2, eventCalledCount);
        }

        public void TestExecutionEvent(object sender, EventArgs e)
        {
            eventCalledCount = eventCalledCount + 1;
        }
	}
}
